package com.frxuexi.park2209

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.webkit.JavascriptInterface
import android.webkit.WebView
import com.frxuexi.park2209.databinding.ActivityWebBinding

class WebActivity : AppCompatActivity() {
    lateinit var binding: ActivityWebBinding
    lateinit var webView: WebView
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding=ActivityWebBinding.inflate(layoutInflater)
        setContentView(binding.root)
        webView=binding.webview
        webView.apply {
            loadUrl("file:///android_asset/test.html")
            settings.javaScriptEnabled=true
            //js调用安卓1.
            addJavascriptInterface(this@WebActivity,"android")
        }

        binding.btnCalljs.setOnClickListener {
            //安卓调js方法1.
            webView.loadUrl("javascript:harmonyCallJS()")
        }


    }

    //js调用安卓2.
    @JavascriptInterface
    fun harmonyosAgree():String{
        return "hello"
    }

    override fun onDestroy() {
        super.onDestroy()
        webView.clearHistory()
        webView.removeJavascriptInterface("android")

    }
}